package com.junjie.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.junjie.entity.Apartment;
import com.junjie.entity.Room;
import com.junjie.entity.User;
import com.junjie.services.ApartmentService;
import com.junjie.services.RoomService;
import com.junjie.services.UserService;
import com.junjie.utils.CommonParams;
import com.junjie.utils.CommonResult;
import com.junjie.utils.JWTUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

@Controller
@RequestMapping("/room")
@ResponseBody
public class RoomController {

    @Autowired
    private RoomService roomService;

    @Autowired
    private UserService userService;

    @Autowired
    private ApartmentService apartmentService;

    //根据房间 AptId 来查询信息
    @RequestMapping(value = "/getRoomsByAptId", method = RequestMethod.GET)
    public CommonResult getRoomsByAptId(@RequestParam("apt_id") Integer AptId, @RequestParam("status") Integer status) {
        Room room = new Room();
        room.setAptId(AptId);
        room.setStatus(status);
        List<Room> rooms = roomService.selectSearchRoom(room);
        return new CommonResult(CommonParams.SUCCESS, "房间信息信息查询成功", rooms);
    }

    //根据根据信息来查询房间
    @RequestMapping(value = "/selectSearchRoom", method = RequestMethod.POST)
    public CommonResult selectSearchRoom(@RequestBody JSONObject params, HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        //获取用户的 id
        String room_id = JWTUtils.getTokenStringValue(token, "room_id");
        //获取公寓的AptId
        Integer AptId = Integer.parseInt(JWTUtils.getTokenStringValue(token, "apt_id"));
        //获取角色id
        int rule_id = Integer.parseInt(JWTUtils.getTokenStringValue(token, "role_id"));
        //封装查询的信息
        Room room = new Room();
        room.setId(params.getInteger("id"));

        Integer ai = params.getInteger("AptId");
        String r_id = params.getString("r_id");
        if ((ai == null) && (r_id == null || r_id.equals(""))) {
            //如果是超级管理员会拿取所有房间的信息
            if(rule_id == 2) {
                //如果是管理员会拿去当前公寓所有的房间
                room.setAptId(AptId);
            } else if(rule_id == 3) {
                //只会拿去当前用户的房间
                room.setRId(room_id);
                room.setAptId(AptId);
            }
        } else {
            room.setRId(params.getString("r_id"));
            room.setAptId(params.getInteger("AptId"));
        }
        room.setPrice(params.getBigDecimal("price"));
        room.setStatus(1);
        Integer pageNum = params.getInteger("pageNum");
        Integer pageSize = params.getInteger("pageSize");

        PageHelper.startPage(pageNum, pageSize);
        List<Room> rooms = roomService.selectSearchRoom(room);
        PageInfo<Room> pageInfo = new PageInfo<>(rooms);

        //获取每个房间的人数
        List l = pageInfo.getList().stream().parallel().peek(e -> {
            User a = new User();
            Apartment apartment = new Apartment();
            apartment.setAId(e.getAptId());
            a.setRoomId(e.getRId());
            List<User> usersByEntity = userService.getUsersByEntity(a);
            String aptName = apartmentService.getOneApartment(apartment).getAptName();
            e.setAptName(aptName);
            e.setNum(usersByEntity.size());
        }).collect(Collectors.toList());

        pageInfo.setList(l);

        System.out.println(pageInfo);
        return new CommonResult(CommonParams.SUCCESS, "房间信息信息查询成功", pageInfo);
    }

    //插入房间的信息
    @RequestMapping(value = "/insertRoom", method = RequestMethod.POST)
    public CommonResult insertRoom(@RequestBody JSONObject params) {
        //封装插入的信息
        Room room = new Room();
        room.setRId(params.getString("rId"));
        room.setAptId(params.getInteger("AptId"));
        room.setType(params.getInteger("type"));
        room.setPrice(params.getBigDecimal("price"));
        room.setStatus(1);
        return roomService.insertRoom(room);
    }

    //修改一间房间的信息
    @RequestMapping(value = "/updateRoomById", method = RequestMethod.POST)
    public CommonResult updateRoomById(@RequestBody JSONObject params) {
        Room room = new Room();
        room.setId(params.getInteger("id"));
        room.setRId(params.getString("r_id"));
        room.setAptId(params.getInteger("AptId"));
        room.setType(params.getInteger("type"));
        room.setPrice(params.getBigDecimal("price"));
        room.setStatus(params.getInteger("status"));
        return roomService.updateRoomById(room);
    }

    //删除一间房间的信息
    @RequestMapping(value = "/deleteRoomById/{id}", method = RequestMethod.POST)
    public CommonResult deleteRoomById(@PathVariable("id") Integer id) {
        return roomService.deleteRoomById(id);
    }
}
